import { defineComponent, computed, getCurrentInstance, ref, watch } from "vue";
import defaultProps from "../props.mjs";
import Compressor from "compressorjs";
const name = "ep-fileupload";
const _sfc_main = defineComponent({
  name,
  props: defaultProps,
  emits: ["update:modelValue"],
  setup(props, { emit }) {
    const isDetail = computed(() => props.options.permission > 0);
    const internalInstance = getCurrentInstance();
    const proxy = internalInstance == null ? void 0 : internalInstance.proxy;
    const fieldRef = ref(null);
    const showViewer = ref(false);
    const preview = computed(() => {
      return Array.isArray(fileList.value) && fileList.value.length > 0;
    });
    const previewIndex = ref(0);
    const imageIndex = computed(() => {
      if (previewIndex.value > fileList.value.length - 1) {
        return 0;
      }
      return previewIndex.value;
    });
    const previewSrcList = computed(() => {
      return fileList.value.map((s) => s.url);
    });
    const prevOverflow = "";
    function closeViewer() {
      document.body.style.overflow = prevOverflow;
      showViewer.value = false;
    }
    const fileList = ref([]);
    const setFileList = (files) => {
      console.log("fileList.value :>> ", fileList.value);
    };
    const downloadFile = (file) => {
      const { actionBlob } = props.options;
    };
    const handleRemove = (file) => {
      if (isDetail.value) {
        return false;
      }
      fileList.value.splice(fileList.value.indexOf(file), 1);
      emit("update:modelValue", fileList.value);
    };
    const handlePictureCardPreview = (file) => {
      if (!preview.value) return;
      const index = fileList.value.findIndex((x) => x === file);
      previewIndex.value = index;
      document.body.style.overflow = "hidden";
      showViewer.value = true;
    };
    const fileMaxSize = computed(() => {
      return Number(props.options.fileMaxSize || 5) * 1024 * 1024;
    });
    const handleBeforeUpload = (file) => new Promise((resolve, reject) => {
      const fileTypes = props.options.fileTypes || [];
      if (fileTypes.length > 0) {
        const extFileName = file.name.substring(file.name.lastIndexOf(".") + 1);
        if (!fileTypes.some((ext) => getIconByFileType(file) === ext)) {
          proxy == null ? void 0 : proxy.$message.error(`不支持格式：${extFileName || file.type}`);
          reject();
        }
      }
      const fileMaxSize2 = props.options.fileMaxSize || 5;
      if (file.size >= Number(fileMaxSize2) * 1024 * 1024) {
        proxy == null ? void 0 : proxy.$message.error(`文件大小已超出：${fileMaxSize2}MB`);
        reject();
      }
      if (props.options.reduce && file.type.includes("image")) {
        new Compressor(file, {
          convertSize: 2e6,
          // 超出大小压缩
          maxWidth: props.options.reduce === 2 ? 1440 : 3024,
          // 输出图像最大宽度
          quality: props.options.reduce === 2 ? 0.8 : 0.9,
          // 压缩质量
          success: resolve,
          error(err) {
            console.log(err.message);
            proxy == null ? void 0 : proxy.$message.error("图片压缩错误！");
            reject();
          }
        });
      } else {
        resolve(file);
      }
    });
    const handleOnSuccess = (response, uploadFile, uploadFiles) => {
      const { id } = response;
      console.log("uploadFile :>> ", uploadFile);
    };
    const handleOnError = () => {
      proxy == null ? void 0 : proxy.$message.error("上传失败！");
    };
    const handleOnExceed = () => {
      proxy == null ? void 0 : proxy.$message.error(`最大上传数量(${props.options.limit})已超出.`);
    };
    const handleHttpRequest = (options) => {
      console.log("options :>> ", options, fileList);
      const file = fileList.value.find((m) => m.uid === options.file.uid);
      if (file) {
        file.status = "uploading";
        file.size = options.file.size;
        const formdata = new FormData();
        formdata.append("file", options.file);
      }
    };
    const hiddenUploadBtn = computed(() => {
      return fileList.value.length >= props.options.limit;
    });
    watch(
      () => props.modelValue,
      (files) => {
        if (files) {
          setFileList();
        }
      },
      { immediate: true }
    );
    const getFileType = (file) => {
      var _a;
      return ((_a = file.raw) == null ? void 0 : _a.type) || "unknown";
    };
    const getIconByFileType = (file) => {
      const type = file.type;
      if (type.includes("image/")) {
        return "jpg";
      }
      if (type === "application/msword") {
        return "doc";
      }
      if (type === "application/vnd.openxmlformats-officedocument.wordprocessingml.document") {
        return "doc";
      }
      if (type === "application/vnd.ms-excel") {
        return "xls";
      }
      if (type === "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet") {
        return "xls";
      }
      if (type === "application/vnd.ms-powerpoint") {
        return "ppt";
      }
      if (type === "application/vnd.openxmlformats-officedocument.presentationml.presentation") {
        return "ppt";
      }
      if (type === "application/x-zip-compressed") {
        return "zip";
      }
      if (type === "application/octet-stream") {
        return "zip";
      }
      return "unknown";
    };
    return {
      isDetail,
      fieldRef,
      preview,
      imageIndex,
      showViewer,
      previewSrcList,
      fileList,
      hiddenUploadBtn,
      fileMaxSize,
      getFileType,
      getIconByFileType,
      downloadFile,
      closeViewer,
      handleRemove,
      handlePictureCardPreview,
      handleBeforeUpload,
      handleOnSuccess,
      handleOnError,
      handleOnExceed,
      handleHttpRequest
    };
  }
});
export {
  _sfc_main as default
};
